/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.workcenter.controller;

import java.util.List;

import javax.validation.Valid;

import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.prod.resource.dao.model.ProdResource;
import com.erp.prod.resource.dao.model.ProdResourceCO;
import com.erp.prod.resource.service.ProdResourceService;
import com.erp.prod.workcenter.dao.model.ProdWorkCenterResource;
import com.erp.prod.workcenter.dao.model.ProdWorkCenterResourceCO;
import com.erp.prod.workcenter.service.ProdWorkCenterResourceService;

@Controller
@RequestMapping("/web/prodWorkCenterResource")
public class ProdWorkCenterResourceWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdWorkCenterResourceWebController.class);
    
    //服务层注入
    @Autowired
    private ProdWorkCenterResourceService prodWorkCenterResourceService;
    @Autowired
    private ProdResourceService prodResourceService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdWorkCenterResourceList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-12-02 18:01:30
     * @author 
     * @param pages
     * @param prodWorkCenterResourceCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkCenterResourceList")
    public String getProdWorkCenterResourceList(Pages pages, ProdWorkCenterResourceCO prodWorkCenterResourceCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(100);
        
        //分页查询数据
        List<ProdWorkCenterResource> prodWorkCenterResourceList = this.prodWorkCenterResourceService.getResourceListByWorkCenterCode(pages, prodWorkCenterResourceCO);
           
        //循环获取资源名
        for(ProdWorkCenterResource prodWorkCenterResource: prodWorkCenterResourceList) {
            prodWorkCenterResource.setResourceName(this.prodResourceService.getDataObject(prodWorkCenterResource.getResourceCode()).getResourceName());
        }
        
        //页面属性设置
        model.addAttribute("prodWorkCenterResourceList", prodWorkCenterResourceList);
        model.addAttribute("pages", pages);
        
        return "prodWorkCenter/tab/resourceTab";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-12-02 18:01:30
     * @author 
     * @param prodWorkCenterResource
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkCenterResource")
    public String getProdWorkCenterResource(ProdWorkCenterResource prodWorkCenterResource, Model model) {
        //查询要编辑的数据
        if(prodWorkCenterResource!=null&&prodWorkCenterResource.getWorkCenterResourceId()!=null) {
            prodWorkCenterResource = this.prodWorkCenterResourceService.getDataObject(prodWorkCenterResource.getWorkCenterResourceId());
        }
        
        //获取资源
        ProdResourceCO prodResourceCO = new ProdResourceCO();
        prodResourceCO.setStatus("Y");
        prodResourceCO.setApproveStatus("APPROVE");
        List<ProdResource> prodResourceList = this.prodResourceService.getDataObjects(prodResourceCO);
        
        //页面属性设置
        model.addAttribute("prodWorkCenterResource", prodWorkCenterResource);
        model.addAttribute("prodResourceList", prodResourceList);
        
        return "prodWorkCenter/pop/addLineModal";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-12-02 18:01:30
     * @author 
     * @param prodWorkCenterResource
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdWorkCenterResource")
    @ResponseBody
    public String editProdWorkCenterResource(@Valid ProdWorkCenterResource prodWorkCenterResource, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        try {
            //对当前编辑的对象初始化默认的字段
            if(prodWorkCenterResource.getWorkCenterResourceId()==null) {
                prodWorkCenterResource.setWorkCenterResourceCode(SnowFlake.getConcurrentId().toString());
            }
            
            //保存编辑的数据
            this.prodWorkCenterResourceService.insertOrUpdateDataObject(prodWorkCenterResource);
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-12-02 18:01:30
     * @author 
     * @param prodWorkCenterResource
     * @return String
     *
     */
    @RequestMapping("deleteProdWorkCenterResource")
    @ResponseBody
    public String deleteProdWorkCenterResource(ProdWorkCenterResource prodWorkCenterResource, RedirectAttributes attr) {
        try {
            //删除数据前验证数据
            if(prodWorkCenterResource!=null&&prodWorkCenterResource.getWorkCenterResourceId()!=null) {
                //删除数据
                this.prodWorkCenterResourceService.deleteDataObject(prodWorkCenterResource);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
}
